﻿@{
    Layout = "~/_SiteLayout.cshtml";
    string showType = "";

    Business.Platform.Organize borganize = new Business.Platform.Organize();
    Data.Model.Organize org=null;
    string id = Request.QueryString["id"];

    string name = string.Empty;
    string type = string.Empty;
    string status = string.Empty;
    string chargeLeader = string.Empty;
    string leader = string.Empty;
    string note = string.Empty;
    Guid orgID;
    string oldXML = string.Empty;
    if(id.IsGuid(out orgID))
    {
        org = borganize.Get(orgID);
        if(org!=null)
        {
            name = org.Name;
            type = org.Type.ToString();
            status = org.Status.ToString();
            chargeLeader = org.ChargeLeader;
            leader = org.Leader;
            note = org.Note;
            oldXML = org.Serialize();
        }
    }

    Validation.RequireFields("Name", "Type", "Status");
    if (IsPost && Validation.IsValid() && org!=null)
    {
        //保存
        if(!Request.Form["Save"].IsNullOrEmpty())
        {
            name = Request.Form["Name"];
            type = Request.Form["Type"];
            status = Request.Form["Status"];
            chargeLeader = Request.Form["ChargeLeader"];
            leader = Request.Form["Leader"];
            note = Request.Form["note"];

            org.Name = name.Trim();
            org.Type = type.AsInt(1);
            org.Status = status.AsInt(0);
            org.ChargeLeader = chargeLeader;
            org.Leader = leader;
            org.Note = note.IsNullOrEmpty() ? null : note.Trim();

            borganize.Update(org);
            Business.Platform.Log.Add("修改了组织机构",string.Format("修改前：{0}修改后：{1}",oldXML,org.Serialize()), Business.Platform.Log.Types.组织机构);
           
            string rid=org.ParentID==Guid.Empty?org.ID.ToString():org.ParentID.ToString();
            
            <script type="text/javascript">
                parent.frames[0].reLoad('@rid');
                alert("保存成功!");
            </script>
            
        }
        
        //移动
        if(!Request.Form["Move1"].IsNullOrEmpty())
        {
            string toOrgID = Request.Form["deptmove"];
            Guid toID;
            if(toOrgID.IsGuid(out toID) && borganize.Move(org.ID, toID))
            {
                Business.Platform.Log.Add("移动了组织机构", "将机构：" + org.ID + "移动到了：" + toID, Business.Platform.Log.Types.组织机构);
                string refreshID=org.ParentID == Guid.Empty ? org.ID.ToString() : org.ParentID.ToString();
                <script type="text/javascript">
                    parent.frames[0].reLoad('@refreshID');
                    parent.frames[0].reLoad('@toOrgID'); 
                    alert('移动成功!');
                </script>
            }
            else
            {
                <script type="text/javascript">
                    alert('移动失败!');
                </script>
            }
        }
        
        //删除
        if(!Request.Form["Delete"].IsNullOrEmpty())
        {
            int i = borganize.DeleteAndAllChilds(org.ID);
            Business.Platform.Log.Add("删除了组织机构及其所有下级共" + i.ToString() + "项", org.Serialize(), Business.Platform.Log.Types.组织机构);
            string refreshID = org.ParentID == Guid.Empty ? org.ID.ToString() : org.ParentID.ToString(); ;
            <script type="text/javascript">
                parent.frames[0].reLoad('@refreshID');
                alert('共删除了'+'@i'+'项!');
            </script>
        }
    }

    string typeRadios = borganize.GetTypeRadio("Type", type, "validate=\"radio\"");
    string statusRadios = borganize.GetStatusRadio("Status", status, "validate=\"radio\"");
}
    <script type="text/javascript">
        var win = new RoadUI.Window();
        var validate = new RoadUI.Validate();
    </script>
    <form action="" method="post">
    <br />
    <table cellpadding="0" cellspacing="1" border="0" width="95%" class="formtable">
        <tr>
            <th style="width:80px;">名称：</th>
            <td><input type="text" id="Name" name="Name" class="mytext" validate="empty,minmax" value="@name" max="100" style="width:75%" /></td>
        </tr>
        <tr>
            <th style="width:80px;">类型：</th>
            <td>@Html.Raw(typeRadios)</td>
        </tr>
        <tr>
            <th style="width:80px;">状态：</th>
            <td>@Html.Raw(statusRadios)</td>
        </tr>
        @if("1"!=Request.QueryString["type"]){
        <tr>
            <th style="width:100px;">分管领导：</th>
            <td><input type="text" class="mymember" value="@chargeLeader" unit="0" dept="0" station="0" user="1" group="0" id="ChargeLeader" name="ChargeLeader" /></td>
        </tr>
        <tr>
            <th style="width:80px;">部门/岗位领导：</th>
            <td><input type="text" class="mymember" value="@leader" unit="0" dept="0" station="0" user="1" group="0" id="Leader" name="Leader" /></td>
        </tr>
        }
        <tr>
            <th style="width:80px;">备注：</th>
            <td><textarea id="Note" name="Note" class="mytext" style="width:90%; height:50px;">@note</textarea></td>
        </tr>

        <tr id="deptmove_tr" style="display:none;">
            <th style="width:80px;">移动到：</th>
            <td>
                <table cellpadding="0" cellspacing="0" border="0">
                    <tr>
                        <td><input type="text" style="width:220px;" class="mymember" id="deptmove" name="deptmove" more="false" user="false" station="true" dept="true" unit="true"/></td>
                        <td><input type="submit" class="mybutton" onclick="return deptmove3();" name="Move1" value="确定移动" /></td>
                    </tr>
                </table>
            </td>
        </tr>

    </table>
    <div style="width:95%; margin:10px auto 10px auto; text-align:center;">
        <input type="button" class="mybutton" onclick="window.location='BodyAdd'+'@Request.Url.Query';" value="添加单位/部门/岗位" id="addChild" />
        <!--input type="button" class="mybutton" value="添加工作组" id="addWorkGroup" /-->
        <input type="button" class="mybutton" onclick="window.location='UserAdd'+'@Request.Url.Query';" value="添加人员" id="addUser" />
        <input type="button" class="mybutton" value="移动" id="move" name="move" onclick="deptmove2();" />
        <input type="button" class="mybutton" value="排序" id="sort" onclick="sort1();" />
        <input type="submit" class="mybutton" onclick="return validate.validateForm(document.forms[0]);" name="Save" value="保存" />
        <input type="submit" class="mybutton" name="Delete" onclick="return confirm('您真的要删除该机构及其下级机构吗?');" value="删除" />
    </div>
    </form>

    <script type="text/javascript">
        var showType = '@showType';
        function del()
        {
            return confirm('真的要删除吗?');
        }
        function deptmove3()
        {
            if ($.trim($("#deptmove").val()) == "")
            {
                alert("请选择要移动到的机构!");
                return false;
            }
            else if ($.trim($("#deptmove").val()) == '@id')
            {
                alert("不能将机构移动到自己!");
                return false;
            }
            return true;
        }
        function deptmove2()
        {
            $('#deptmove_tr').toggle();
        }

        function sort1()
        {
            window.location = 'Sort' + '@Html.Raw(Request.Url.Query)';
        }
    </script>